/**
 * Author:江帆
 * Description: Job的增删查改
 * Date:
 */
package service

import (
	"fmt"
	"kubernate-server/conf"
	"net/http"

	batchv1 "k8s.io/api/batch/v1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/utils/pointer"

	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type JobService struct {
}

/*
*	获取job的详情信息
 */
func (*JobService) GetJobDetail(ctx *gin.Context) {
	jobDetail, err := conf.KuberConfigSet.BatchV1().Jobs(ctx.Param("nameSpace")).Get(ctx, ctx.Param("jobName"), metav1.GetOptions{})
	fmt.Println(ctx.Param("nameSpace"), ctx.Param("jobName"))
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"messge": "failure",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"message":   "success",
		"name":      jobDetail.Name,
		"namespace": jobDetail.Namespace,
		"status":    jobDetail.Status,
	})
}

/*
*	获取job列表
 */
func (*JobService) GetJobList(ctx *gin.Context) {
	jobList, err := conf.KuberConfigSet.BatchV1().Jobs(ctx.Param("nameSpace")).List(ctx, metav1.ListOptions{})

	if err != nil {
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"messge":  "success",
		"podList": jobList,
	})
}

/*
*	添加一个job
 */
func (*JobService) CreateJob(ctx *gin.Context) {
	_, error := conf.KuberConfigSet.BatchV1().Jobs(ctx.Param("nameSpace")).Create(ctx, creatJobReq2K8s(ctx), metav1.CreateOptions{})
	if error != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"messge": "failure",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"messge": "success",
	})
}

// 转换k8s数据结构
func creatJobReq2K8s(ctx *gin.Context) *batchv1.Job {
	return &batchv1.Job{
		ObjectMeta: metav1.ObjectMeta{
			Name:      ctx.Param("jobName"),
			Namespace: ctx.Param("nameSpace"),
		},
		Spec: batchv1.JobSpec{
			BackoffLimit: pointer.Int32Ptr(4),
			Template: corev1.PodTemplateSpec{
				Spec: corev1.PodSpec{
					RestartPolicy: corev1.RestartPolicyNever,
					Containers: []corev1.Container{
						{
							Name:  "example-container",
							Image: "nginx:1.15-alpine",
							Command: []string{
								"tail -f /etc/hosts;",
							},
						},
					},
				},
			},
		},
	}
}

/*
*	更新一个job
 */
func (*JobService) UpdateJob(ctx *gin.Context) {
	newJobDetail, err := conf.KuberConfigSet.BatchV1().Jobs(ctx.Param("nameSpace")).Get(ctx, ctx.Param("jobName"), metav1.GetOptions{})
	newJobDetail.Annotations = map[string]string{
		"updated-by": "client-go",
	}
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"message": fmt.Sprintf("获取Job失败: %v", err),
		})
		return
	}

	jobNS := conf.KuberConfigSet.BatchV1().Jobs(ctx.Param("nameSpace"))
	_, err02 := jobNS.Update(ctx, newJobDetail, metav1.UpdateOptions{})
	if err02 != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"messge": "failure",
		})
		return
	}
	if err02 != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"message": fmt.Sprintf("更新Job失败: %v", err),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"messge": "success",
	})
}

/*
*	删除Job
 */
func (*JobService) DeleteJob(ctx *gin.Context) {
	var err = conf.KuberConfigSet.BatchV1().Jobs(ctx.Param("nameSpace")).Delete(ctx, ctx.Param("jobName"), metav1.DeleteOptions{})
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"message": fmt.Sprintf("获取Job失败: %v", err),
		})
		return
	}
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"messge": "failure",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"messge": "success",
	})
}
